<?php
//
// +---------------------------------------------------------------------------------+
// | Package: EezeeSite Core                                                         |
// +---------------------------------------------------------------------------------+
// | Copyright (C) 2006  EezeeSite Development Team                                  |
// +---------------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or                   |
// | modify it under the terms of the GNU General Public License                     |
// | as published by the Free Software Foundation; either version 2                  |
// | of the License, or (at your option) any later version.                          |
// |                                                                                 |
// | This program is distributed in the hope that it will be useful,                 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                  |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                   |
// | GNU General Public License for more details.                                    |
// |                                                                                 |
// | You should have received a copy of the GNU General Public License               |
// | along with this program; if not, write to the Free Software                     |
// | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. |
// +---------------------------------------------------------------------------------+
// | Author: Mayukh De <mayukhde@gmail.com>                                          |
// +---------------------------------------------------------------------------------+
//

class ESWebForms
{
    var $extraData;
    var $form;
    var $validationType;
    var $type;
    
    function buildForm($type, $extraData = null, $validationType = null)
    {
        global $ESResponse;
        $this->type = $type;
        $this->extraData = (is_array($extraData)) ? $extraData : null;
        $this->validationType = ($validationType && ($validationType == 'server' || $validationType == 'client')) ? $validationType : 'client';
        $action = ($extraData['FORMACTION']) ? $extraData['FORMACTION'] : '';
        $attributes = ($extraData['FORMONEVENTACTION'] && is_array($extraData['FORMONEVENTACTION'])) ? $extraData['FORMONEVENTACTION'] : null;
        $this->form = new ESFormsGenerator($type, 'post', $action, null, $attributes);
        $this->form->removeAttribute('name');
        if (method_exists($this, $type)) {
            call_user_func(array(&$this, $type));
        } else {
            $ESResponse->raiseESError(sprintf($GLOBALS['ES']['LANG']['SYSERROR']['NOWEBFORM'], $type), 'display');
        }
        $renderer =& new HTML_QuickForm_Renderer_Tableless();
        $renderer->_elementTemplate = "\n\t<div class=\"qfrow\"><label class=\"qflabel\"><!-- BEGIN required --><span class=\"required\">*</span><!-- END required -->{label}</label><div class=\"qfelement\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div><br />";
        $renderer->_requiredNoteTemplate = "\n\t<div class=\"qfrow\"><label class=\"qflabel\">&nbsp;</label><div class=\"qfelement\">{requiredNote}</div>";
        $renderer->addStopFieldsetElements('submit');
        $this->form->accept($renderer);
        return $renderer->toHtml();        
    }
    
    function installAuthForm()
    {
        $this->form->addElement('text', 'admin_name', 'Admin '.$GLOBALS['ES']['LANG']['AUTH']['FORM']['USER'], array('size' => 20));
        $this->form->addElement('password', 'admin_password', $GLOBALS['ES']['LANG']['AUTH']['FORM']['PASS'], array('size' => 20));
        $this->form->addRule('admin_name', 'Admin '.$GLOBALS['ES']['LANG']['AUTH']['FORM']['USER'].$GLOBALS['ES']['LANG']['FORMRULES']['REQUIRED'], 'required', null, $this->validationType);
        $this->form->addRule('admin_password', 'Admin '.$GLOBALS['ES']['LANG']['AUTH']['FORM']['PASS'].$GLOBALS['ES']['LANG']['FORMRULES']['REQUIRED'], 'required', null, $this->validationType);
        $this->form->addElement('submit', null, 'Log In');
        //$this->form->addElement('button', null, 'Log In', array('onclick' => 'submitForm('.__FUNCTION__.'); return false;'));
        return;
    }
    
    function configCreateForm()
    {
        $this->form->setDefaults(
                                array(
                                     'admin_group' => 'Administrator'
                                     )
                                );
        $this->form->addElement('text', 'admin_name', 'Admin Name', array('size' => 20));
        $this->form->addElement('password', 'admin_password', 'Admin Password', array('size' => 20));
        $this->form->addElement('text', 'admin_email', 'Admin Email', array('size' => 20));
        $s1 = $this->form->createElement('select', 'db_type', 'DB Type');
        $s1->loadArray($GLOBALS['ES']['DBSUPPORT'], 'MYSQL');
        $this->form->addElement($s1);
        $this->form->addElement('text', 'db_host', 'DB Host', array('size' => 20));
        $this->form->addElement('text', 'db_name', 'DB Name', array('size' => 20));
        $this->form->addElement('text', 'db_user_name', 'DB User Name', array('size' => 20));
        $this->form->addElement('text', 'db_password', 'DB Password', array('size' => 20));
        $this->form->addElement('text', 'db_port', 'DB Port', array('size' => 20));
        $this->form->addElement('text', 'table_prefix', 'DB Table Prefix', array('size' => 20));
        $this->form->addElement('text', 'site_title', 'Website Title', array('size' => 20));
        $this->form->addElement('text', 'logo_src', 'Logo Image Name', array('size' => 20));
        $this->form->addElement('text', 'catch_line', 'Catchline', array('size' => 20));
        $this->form->addElement('text', 'cookie_name', 'Cookie Name', array('size' => 20));
        $this->form->addElement('text', 'primary_email', 'Website Primary Email', array('size' => 20));
        $this->form->addElement('text', 'admin_group', 'Admin Usergroup Name', array('size' => 20));
        $this->form->addElement('text', 'member_group', 'Member Usergroup Name', array('size' => 20));
        $s2 = $this->form->createElement('select', 'post_signup_action', 'After a user registers on the site...');
        $s2->loadArray($GLOBALS['ES']['POSTSIGNUPOPTIONS'], 'SENDACTIVATIONMAIL');
        $this->form->addElement($s2);
        if ($GLOBALS['ES']['ALLOWCLIENT']['REWRITEURLS']) {
            $this->form->addElement('checkbox', 'rewrite_url', 'Rewrite URLs?');
        }
        if ($GLOBALS['ES']['ALLOWCLIENT']['USECACHEING']) {
            $this->form->addElement('checkbox', 'use_cache', 'Allow cacheing of static pages?');
        }
        $this->form->addElement('submit', null, 'Submit');
        $this->form->addRule('admin_name', 'Admin name is required', 'required', null, $this->validationType);
        $this->form->addRule('admin_name', 'Admin name may contain letters and numbers only', 'alphanumeric', null, $this->validationType);
        $this->form->addRule('admin_password', 'Admin password name is required', 'required', null, $this->validationType);
        $this->form->addRule('admin_email', 'Admin email is required', 'required', null, $this->validationType);
        $this->form->addRule('db_host', 'DB hostname is required', 'required', null, $this->validationType);
        $this->form->addRule('db_name', 'DB name is required', 'required', null, $this->validationType);
        $this->form->addRule('table_prefix', 'DB table prefix is required', 'required', null, $this->validationType);
        $this->form->addRule('site_title', 'Website title is required', 'required', null, $this->validationType);
        $this->form->addRule('cookie_name', 'Cookie name is required', 'required', null, $this->validationType);
        $this->form->addRule('primary_email', 'Website primary email is required', 'required', null, $this->validationType);
        $this->form->addRule('admin_email', 'Admin email must be of a generic email pattern', 'email', null, $this->validationType);
        $this->form->addRule('primary_email', 'Website primary email must be of a generic email pattern', 'email', null, $this->validationType);
        $this->form->addRule('admin_group', 'Admin usergroup name is required', 'required', null, $this->validationType);
        $this->form->addRule('member_group', 'Member usergroup name is required', 'required', null, $this->validationType);
        $this->form->addRule('table_prefix', 'DB table prefix should contain only letters', 'lettersonly', null, $this->validationType);
        $this->form->addRule('db_port', 'DB port should contain only numbers', 'numbersonly', null, $this->validationType);
        return;
    }
    
    function DBCreateForm()
    {
        $this->form->setDefaults(
                                array(
                                     'db_host'      => $GLOBALS['ES']['CLIENTCONF']['db_host'],
                                     'db_name'      => $GLOBALS['ES']['CLIENTCONF']['db_name'],
                                     'db_user_name' => $GLOBALS['ES']['CLIENTCONF']['db_user_name'],
                                     'db_password'  => $GLOBALS['ES']['CLIENTCONF']['db_password'],
                                     'db_port'      => $GLOBALS['ES']['CLIENTCONF']['db_port'],
                                     //'db_action'    => 2
                                     )
                                );
        $this->form->addElement('text', 'db_host', 'DB Host', array('size' => 20));
        $this->form->addElement('text', 'db_name', 'DB Name', array('size' => 20));
        $this->form->addElement('text', 'db_user_name', 'DB User Name', array('size' => 20));
        $this->form->addElement('text', 'db_password', 'DB Password', array('size' => 20));
        $this->form->addElement('text', 'db_port', 'DB Port', array('size' => 20));
        $this->form->addElement('radio', 'db_action', 'The DB info is OK', null, 1);
        $this->form->addElement('radio', 'db_action', 'Create the DB', null, 2);
        /*$radio = array();
        $radio[] = &HTML_QuickForm::createElement('radio', 'db_action', null, 'The DB info is OK. Try to connect.');
        $radio[] = &HTML_QuickForm::createElement('radio', 'db_action', null, 'Create the DB');
        $this->form->addElement($radio, 'db_action_radios', 'What to do with the database', '<br />');*/
        $this->form->addElement('submit', null, 'Submit');
        $this->form->addRule('db_host', 'DB hostname is required', 'required', null, $this->validationType);
        $this->form->addRule('db_name', 'DB name is required', 'required', null, $this->validationType);        
        return;
    }
    
    function logInForm()
    {
        $this->form->addElement('text', 'username', $GLOBALS['ES']['LANG']['AUTH']['FORM']['EMAIL'], array('size' => 20));
        $this->form->addElement('password', 'password', $GLOBALS['ES']['LANG']['AUTH']['FORM']['PASS'], array('size' => 20));
        $this->form->addRule('username', $GLOBALS['ES']['LANG']['AUTH']['FORM']['EMAIL'].$GLOBALS['ES']['LANG']['FORMRULES']['REQUIRED'], 'required', null, $this->validationType);
        $this->form->addRule('password', $GLOBALS['ES']['LANG']['AUTH']['FORM']['PASS'].$GLOBALS['ES']['LANG']['FORMRULES']['REQUIRED'], 'required', null, $this->validationType);
        $this->form->addElement('submit', null, 'Log In');
        $this->form->addElement('html', '<div class="qfrow"><label class="qflabel">&nbsp;</label><div class="qfelement"><a href="#passwordreminder" onclick="showFloatingContent(\'Forgot Your Password?\', \'passwordreminderbox\'); hideFloatingContent(\'loginbox\');"><b>Forgot password?</b></a></div>');
        return;
    }
    
    function passwordReminderForm()
    {
        $this->form->addElement('text', 'user', $GLOBALS['ES']['LANG']['AUTH']['FORM']['USER'], array('size' => 20));
        $this->form->addRule('user', $GLOBALS['ES']['LANG']['AUTH']['FORM']['USER'].$GLOBALS['ES']['LANG']['FORMRULES']['REQUIRED'], 'required', null, $this->validationType);
        $this->form->addElement('submit', null, 'Give me a new password');
        return;
    }
    
    function newUserRegisterForm()
    {
        $range = array(6, 10);
        $img = $this->form->getCAPTCHA();
        $this->form->setDefaults(
                                array(
                                     'date_of_birth' => array(
                                                             'M' => date('n', time()),
                                                             'd' => date('d', time()),
                                                             'Y' => date('Y', time()) - 18
                                                             )
                                     )
                                );
        $this->form->addElement('text', 'user_name', 'Username', array('size' => 20));
        $this->form->addElement('password', 'user_password', 'Password', array('size' => 20));
        $this->form->addElement('password', 'user_password_repeat', 'Repeat Password', array('size' => 20));
        $this->form->addElement('text', 'user_email', 'Email Address', array('size' => 20));
        $this->form->addElement('text', 'first_name', 'First Name', array('size' => 20));
        $this->form->addElement('text', 'last_name', 'Last Name', array('size' => 20));
        $this->form->addElement('date', 'date_of_birth', 'Date of Birth', $GLOBALS['ES']['CALENDAR']);
        $this->form->addElement('text', 'location', 'Location', array('size' => 20));
        $this->form->addElement('checkbox', 'updates', 'Receive email updates/newsletters?');
        $this->form->addElement('checkbox', 'searchable', 'Be listed in user searches?');
        $this->form->addElement('text', 'phrase_check', 'The text as it appears in the image below', array('size' => 20));
        $this->form->addElement('html', '<div class="qfrow"><label class="qflabel">&nbsp;</label><div class="qfelement"><img src="'.ES_ASSETS_URL.$img.'" alt="CAPTCHA" /></div>');
        //$this->form->addElement('html', '<div class="qfrow"><label class="qflabel">&nbsp;</label><div class="qfelement"><a href="javascript:submitForm(\''.$this->type.'\');"><b>Submit</b></a></div>');
        $this->form->addElement('submit', null, 'Sign Up');
        $this->form->addRule('user_name', 'Username is required', 'required', null, $this->validationType);
        $this->form->addRule('user_name', 'Username may contain letters and numbers only', 'alphanumeric', null, $this->validationType);
        $this->form->addRule('user_name', 'Username should be between '.$range[0].' and '.$range[1].' characters', 'rangelength', $range, $this->validationType);
        $this->form->addRule('user_password', 'Password is required', 'required', null, $this->validationType);
        $this->form->addRule('user_password', 'Password should be between '.$range[0].' and '.$range[1].' characters', 'rangelength', $range, $this->validationType);
        $this->form->addRule('user_password_repeat', 'Repeat password is required', 'required', null, $this->validationType);
        $this->form->addRule(array('user_password', 'user_password_repeat'), 'The passwords do not match', 'compare', null, $this->validationType);
        $this->form->addRule('user_email', 'Email is required', 'required', null, $this->validationType);
        $this->form->addRule('user_email', 'Email must be of a generic email pattern', 'email', null, $this->validationType);
        $this->form->addRule('phrase_check', 'The text as it appears in the image is required', 'required', null, $this->validationType);
    }
}

?>